package com.winhxd.b2c.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.system.org.condition.SysOrgCondition;
import com.winhxd.b2c.common.domain.system.org.enums.SysOrgStatusEnum;
import com.winhxd.b2c.common.domain.system.user.condition.SysUserCondition;
import com.winhxd.b2c.common.domain.system.user.model.SysOrg;
import com.winhxd.b2c.common.exception.BusinessException;
import com.winhxd.b2c.system.dao.SysOrgMapper;
import com.winhxd.b2c.system.dao.SysUserMapper;
import com.winhxd.b2c.system.service.SysOrgService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 部门service
 * @author shumin
 * @create 2019/1/7 0007 14:40
 */
@Service
public class SysOrgServiceImpl implements SysOrgService {

    private static final Logger logger = LoggerFactory.getLogger(SysOrgServiceImpl.class);

    @Autowired
    private SysOrgMapper sysOrgMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public PagedList<SysOrg> findOrgPageList(SysOrgCondition condition) {
        Page<Object> page = PageHelper.startPage(condition.getPageNo(), condition.getPageSize());
        List<SysOrg> list = sysOrgMapper.selectOrgPageList(condition);
        PagedList<SysOrg> result = new PagedList<>();
        result.setData(list);
        result.setPageNo(condition.getPageNo());
        result.setPageSize(condition.getPageSize());
        result.setTotalRows(page.getTotal());
        return result;
    }

    @Override
    public void saveOrg(SysOrg sysOrg) {
        //设置时间信息
        logger.info("新增部门, 参数：sysOrg={}", sysOrg);
        sysOrg.setStatus(SysOrgStatusEnum.NORMAL.getIdentity());
        sysOrgMapper.insertSelective(sysOrg);
    }

    @Override
    public void editOrg(SysOrg sysOrg) {
        logger.info("修改部门, 参数：sysOrg={}", sysOrg);
        sysOrgMapper.updateByPrimaryKeySelective(sysOrg);
    }

    @Override
    public void deleteOrg(SysOrg sysOrg) {
        logger.info("逻辑删除部门, 参数：id={}", sysOrg.getId());
        //删除部门前,需要部门下属没有员工, 否则返回 "该部门不可删除，请先删除或修改员工信息"
        //查询该部门是否有员工
        SysUserCondition condition = new SysUserCondition();
        condition.setOrgId(sysOrg.getId());
        int count = sysUserMapper.countUserByOrgId(sysOrg.getId());
        if(count > 0){
            //该部门不可删除，请先删除或修改员工信息
            throw new BusinessException(BusinessCode.CODE_200024);
        }
        //设置状态为 2 (删除状态)
        SysOrg entity = new SysOrg();
        entity.setId(sysOrg.getId());
        entity.setStatus(SysOrgStatusEnum.DELETE.getIdentity());
        entity.setUpdated(new Date());
        entity.setUpdatedBy(sysOrg.getUpdatedBy());
        entity.setUpdatedByName(sysOrg.getUpdatedByName());
        sysOrgMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public SysOrg getById(Long id) {
        return sysOrgMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SysOrg> findOrgList(SysOrgCondition condition) {
        return sysOrgMapper.selectOrgPageList(condition);
    }
}
